<html>

<head>
</head>
 
<body>
 <?php
	$idPost;
	if(isset($_GET['idPost'])){
		
		$idPost = $_GET['idPost'];
		
		$doc = new DomDocument();
		$doc->load('blog.xml');
		
		$posts 		 = $doc->getElementsByTagName('post');
		$comentarios = $doc->getElementsByTagName('comentario');
		$totalComentarios = 0;
		
		
		foreach($posts as $post){
			$getIdPost = $post->getAttribute('idPost');
			
			if($idPost == $getIdPost){			
				$titulo 	= $post->getElementsByTagName('titulo')->item(0)->nodeValue;
				$conteudo	= $post->getElementsByTagName('conteudo')->item(0)->nodeValue;
				$data	    = $post->getElementsByTagName('data')->item(0)->nodeValue;
				
				echo "<table width=\"700\" cellspacing=\"0\" cellpadding=\"0\">
				      <tr><td width=\"550\"><h4>{$titulo}</h4><td width=\"150\">{$data}<td></tr>
							  <tr><td colspan=\"2\"><hr /></td></tr>
							  <tr><td width=\"600\" colspan=\"2\"><p>{$conteudo}</p><br /></td></tr></table>";
			}	
		}	
				foreach($comentarios as $comentario){
					$idPostComentario = $comentario->getAttribute('idPostComentario');		
					if($idPost == $idPostComentario){
						$conteudoComentario = $comentario->getElementsByTagName('conteudo')->item(0)->nodeValue;
						$autor 				= $comentario->getElementsByTagName('autor')->item(0)->nodeValue;
						
						echo "<table width=\"700\" cellspacing=\"0\" cellpadding=\"0\">
							  <tr><td width=\"550\"><h5>{$autor}</h5><td width=\"150\">{$data}<td></tr>
							  <tr><td width=\"600\" colspan=\"2\"><p>{$conteudoComentario}</p><br /></td></tr></table>";
						$totalComentarios++;
					}else{
						$totalComentarios = 0;
					}
				}
				if($totalComentarios == 0){
					echo "Nenhum comentario para esse post! seja o primeiro.";
				}
	}
	
	if($_POST){
		$autor = $_POST[txtAutor];
		$conteudo = $_POST[txtConteudo];
		$data = date('d/m/Y H:i:s');
		
		$doc = new DomDocument();
		$doc->load('blog.xml');
		
		$posts 				   		 = $doc->getElementsByTagName('posts')->item(0);
		$idComentario 				 = $doc->getElementsByTagName('comentario');

		foreach($idComentario as $Id){
			$ultimoId = $Id->getAttribute('idComentario') + 1;
		}
		$comentarios 		  		 = $posts->appendChild($doc->createElement('comentario'));
		$comentarioAttrId 	   		 = $doc->createAttribute('idComentario');
		$comentarioAttrIdPost        = $doc->createAttribute('idPostComentario');
		$comentarioAttrId->value     = $ultimoId;
		$comentarioAttrIdPost->value = $idPost;
		$comentarios->appendChild($comentarioAttrId);
		$comentarios->appendChild($comentarioAttrIdPost);
		$comentarios->appendChild($doc->createElement('autor',$autor));
		$comentarios->appendChild($doc->createElement('conteudo',$conteudo));
		$comentarios->appendChild($doc->createElement('data', $data));
		if($doc->save('blog.xml')){
			echo "<script>alert('Operaoção realizada com sucesso!'); window.location = 'detalhePost.php?idPost={$idPost}';</script>";
		}
	}
 ?>
 
<form action="detalhePost.php" method="post">
	Nome:
	<br />
	<input type="text" name="txtAutor" />
	<br />
	<br />
	Comentario:
	<br />
	<textarea cols="50" rows="20" name="txtConteudo"></textarea>
	<br />
	<br />
	<input type="submit" name="enviar" id="enviar" value="Comentar!" />
</form>
 </body>
 </html>